﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using NetJsWire.Direct.exceptions;
using NetJsWire.Direct.helper;
using Newtonsoft.Json;

namespace NetJsWire.Direct
{
    /// <summary>
    /// </summary>
    public class DirectProvider
    {

        public const string REMOTING_PROVIDER = "remoting";

        private readonly Dictionary<string, DirectAction> actions;
        private string api = string.Empty;

        /// <summary>
        ///   Creates an instance of the object.
        /// </summary>
        /// <param name = "name">The name of the provider.</param>
        /// <param name = "url">The url of the provider.</param>
        public DirectProvider( string name, string url )
            : this( name, url, REMOTING_PROVIDER )
        {
        }

        /// <summary>
        ///   Creates an instance of the object.
        /// </summary>
        /// <param name = "name">The name of the provider.</param>
        /// <param name = "url">The url of the provider.</param>
        /// <param name = "type">The type of the provider.</param>
        public DirectProvider( string name, string url, string type )
        {
            Name = name;
            Url = url;
            Type = type;

            actions = new Dictionary<string, DirectAction>();
        }

        /// <summary>
        ///   Indicates whether the provider has been configured or not.
        /// </summary>
        public bool Configured
        {
            get;
            private set;
        }

        /// <summary>
        ///   Gets/sets the name of the provider.
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        ///   Gets/sets the url to router requests to for this provider.
        /// </summary>
        public string Url
        {
            get;
            set;
        }

        /// <summary>
        ///   Gets/sets the type of the provider.
        /// </summary>
        public string Type
        {
            get;
            set;
        }

        /// <summary>
        ///   Configure the provider by adding the available API methods.
        /// </summary>
        /// <param name = "assembly">The assembly to automatically generate parameters from.</param>
        public void Configure( Assembly assembly )
        {
            if ( !Configured )
            {
                var types = new List<Type>();
                foreach ( var type in assembly.GetTypes() )
                {
                    types.Add( type );
                }
                Configure( types );
            }
        }


        /// <summary>
        ///   Configure the provider by adding the available API methods.
        /// </summary>
        /// <param name = "items">A series of object instances that contain Ext.Direct methods.</param>
        public void Configure( Assembly[] assemblyList )
        {

            if ( !Configured )
            {
                var types = new List<Type>();
                foreach ( var curAssembly in assemblyList )
                {
                    foreach ( var type in curAssembly.GetTypes() )
                    {

                        types.Add( type );
                    }
                }

                Configure( types );
            }
        }

        /*
         public void Configure(Type[] typelist)
         {

             if (!this.Configured)
             {
                 List<object> types = new List<object>();
                 foreach (var allAssembly in assemblyList)
                 {
                     if (allAssembly != null)
                     {
                         types.AddRange(allAssembly.GetTypes());
                     }
                 }

                 this.Configure(types);
             }
         }*/
        /// <summary>
        ///   Configure the provider by adding the available API methods.
        /// </summary>
        /// <param name = "items">A series of object instances that contain Ext.Direct methods.</param>
        /// <summary>
        ///   Configure the provider by adding the available API methods.
        /// </summary>
        /// <param name = "items">A series of object instances that contain Ext.Direct methods.</param>
        public void Configure( IEnumerable<object> items )
        {
            if ( !Configured )
            {
                var types = new List<Type>();
                foreach ( var item in items )
                {
                    if ( item != null )
                    {
                        types.Add( item.GetType() );
                    }
                }
                Configure( types );
            }
        }

        //Generic configuration method for a list of types.
        private void Configure( IEnumerable<Type> types )
        {
            foreach ( var type in types )
            {
                if ( type.Name == "Class1" )
                {
                    var test = "";
                }
                if ( DirectAction.IsAction( type ) )
                {
                    actions.Add( type.Name, new DirectAction( type ) );
                }
            }
            Configured = true;
        }

        /// <summary>
        ///   Clears any previous configuration for this provider.
        /// </summary>
        public void Clear()
        {
            Configured = false;
            actions.Clear();
        }

        public override string ToString()
        {
            if ( Configured && String.IsNullOrEmpty( api ) )
            {
                using ( var sw = new StringWriter() )
                {
                    using ( var jw = new JsonTextWriter( sw ) )
                    {
                        jw.WriteStartObject();
                        Utility.WriteProperty( jw, "type", Type );

                        Utility.WriteProperty( jw, "id", "1" );
                        // Utility.WriteProperty<int>(jw, "enableBuffer", 3000);
                        Utility.WriteProperty( jw, "url", Url );
                        jw.WritePropertyName( "actions" );
                        jw.WriteStartObject();
                        foreach ( var action in actions.Values )
                        {
                            action.Write( jw );
                        }
                        jw.WriteEndObject();
                        jw.WriteEndObject();
                        api = String.Format( "{0} = {1};", Name, sw.ToString() );
                    }
                }
            }
            return api;
        }

        /// <summary>
        ///   Finds the action in the assemblys
        /// </summary>
        /// <param name = "request">the request you want to find the assembly to</param>
        /// <returns></returns>
        internal DirectAction GetDirectAction( DirectRequest request )
        {
            var action = actions[ request.Action ];
            if ( action == null )
            {
                throw new DirectException( "Unable to find action, " + request.Action, request );
            }
            return action;
        }

        internal DirectMethod GetDirectMethod( DirectRequest request )
        {
            var action = GetDirectAction( request );
            var method = action.GetMethod( request.Method );
            if ( method == null )
            {
                throw new DirectException( "Unable to find method, " + request.Method + " in Action: " + request.Action, request );
            }
            return method;
        }

        internal object Execute( DirectRequest request )
        {
            var action = actions[ request.Action ];
            if ( action == null )
            {
                throw new DirectException( "Unable to find action, " + request.Action, request );
            }
            var method = action.GetMethod( request.Method );
            if ( method == null )
            {
                throw new DirectException( "Unable to find method, " + request.Method + " in Action: " + request.Action, request );
            }
            var type = action.Type;
            return "";//; method.Method.Invoke(type.Assembly.CreateInstance(type.FullName), request.Data);
        }



    }
}
